/*
 * DS10A Message Parser
 */

package x10.interfaces.cm15a.receive;

import x10.utilities.ChunkOfBytes;
import x10.enums.Interfaces;
import x10.enums.Security;
import x10.event.X10Event;
import x10.event.X10SecurityCommand;
import x10.event.X10SecuritySensor;
import x10.utilities.Utils;

/**
 *
 * @author Denny
 */
public class RFSecurityDeviceMessage {
    private byte batteryMask = (byte)0x01;
    private byte commandMask = (byte)0x06;
    private byte contactClosedMask = (byte)0x80;
    private byte delayMask = (byte)0x04;

    private byte statusByte;
    private ChunkOfBytes bytes;

    public RFSecurityDeviceMessage(ChunkOfBytes bytes){
        this.bytes = bytes;
        statusByte = bytes.getBytes()[4];
    }

    public X10Event parse(){
        X10Event device = null;
        if(isSensor()){
            X10Event sensor = new X10SecuritySensor(getSecurityCode(), getContactStatus(), getMotionStatus(), getDelayStatus(), getBatteryStatus(), Interfaces.CM15A.toString());
            device = sensor;
        } else if(isCommand()){
            X10Event command = new X10SecurityCommand(getSecurityCode(), getCommand(), Interfaces.CM15A.toString());
            device = command;
        }
        return device;
    }

    public boolean isCommand(){
        if((statusByte & commandMask) == commandMask){
            return true;
        } else {
            return false;
        }
    }

    public boolean isSensor(){
        return !isCommand();
    }

    private Security getMotionStatus(){
        if(statusByte == (byte)0x0C){
            return Security.MOTION_DETECTED;
        } else {
            return Security.NO_MOTION;
        }
    }

    private Security getCommand(){
        Security returnString = null;

        if(statusByte == (byte)0x06){
            returnString = Security.ARM_AWAY;
        } else if (statusByte == (byte)0x86) {
            returnString = Security.DISARM;
        } else if (statusByte == (byte)0x46) {
            returnString = Security.SECURITY_LIGHTS_ON;
        } else if (statusByte == (byte)0xC6) {
            returnString = Security.SECURITY_LIGHTS_OFF;
        } else if (statusByte == (byte)0x26) {
            returnString = Security.PANIC;
        } else {
            returnString = Security.UNKNOWN;
        }

        return returnString;
    }

    public String getSecurityCode(){
        //Security code is in bytes 2 and 6 of the message

        byte high = bytes.getBytes()[2];
        byte low = bytes.getBytes()[6];

        return String.valueOf(Utils.getIntFromTwoBytes(high, low));

    }

    public Security getContactStatus(){
        if((statusByte & contactClosedMask) == contactClosedMask){
            return Security.CONTACT_CLOSED;
        } else {
            return Security.CONTACT_OPEN;
        }
    }

    public Security getDelayStatus(){
        if((statusByte & delayMask) == delayMask){
            return Security.DELAY_MIN;
        } else {
            return Security.DELAY_MAX;
        }
    }

    public Security getBatteryStatus(){
        //0 = Battery Good, 1 = Battery Low
        if((statusByte & batteryMask) == batteryMask){
            return Security.BATTERY_LOW;
        } else {
            return Security.BATTERY_GOOD;
        }
    }
}
